package com.googlecode.stemwijzer.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.stemwijzer.client.presenter.RegistrationPresenter;

public class RegistrationView extends Composite implements RegistrationPresenter.Display {

	interface RegistrationViewUiBinder extends UiBinder<HTMLPanel, RegistrationView> {
	}

	private static RegistrationViewUiBinder uiBinder = GWT.create(RegistrationViewUiBinder.class);

	@UiField
	TextBox enterUserName;

	@UiField
	Button sendRegistration;

	public RegistrationView() {
		initWidget(uiBinder.createAndBindUi(this));

		this.enterUserName.setText("Naam?");
		this.sendRegistration.setText("Registreer");
	}

	@Override
	protected void onLoad() {
		super.onLoad();

		this.enterUserName.setFocus(true);
		this.enterUserName.selectAll();
	}

	@UiHandler("enterUserName")
	void onKeyPress(KeyPressEvent event) {
		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
			this.sendRegistration.click();
		}
	}

	@Override
	public HasText getName() {
		return this.enterUserName;
	}

	@Override
	public HasClickHandlers getSendRegistration() {
		return this.sendRegistration;
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void startProcessing() {
		this.enterUserName.setEnabled(false);
		this.sendRegistration.setEnabled(false);
	}

	@Override
	public void stopProcessing() {
		this.enterUserName.setEnabled(true);
		this.sendRegistration.setEnabled(true);
	}
}
